package com.car.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import com.car.order.entity.ServeOrderLog;
import com.car.order.entity.form.serveOrderItem.ServeOrderItemFindListForm;
import com.car.order.entity.form.serveOrderItem.ServeOrderItemFindPageForm;
import com.car.order.entity.form.serveOrderItem.ServeOrderItemInsertForm;
import com.car.order.entity.form.serveOrderItem.ServeOrderItemUpdateForm;
import com.car.commons.core.model.WoniuException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.order.entity.param.stock.StockUpdateParam;
import com.car.order.mapper.ServeOrderItemMapper;
import com.car.order.entity.ServeOrderItem;
import com.car.order.mapper.ServeOrderLogMapper;
import com.car.order.mapper.StockLogMapper;
import com.car.order.mapper.StockMapper;
import com.car.order.service.ServeOrderItemService;
import com.car.order.service.StockService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 杜岷芫
 * @since 2024-08-29 10:20:36
 */
@Service
@Slf4j
public class ServeOrderItemServiceImpl extends ServiceImpl<ServeOrderItemMapper, ServeOrderItem> implements ServeOrderItemService {

    @Resource
    private ServeOrderItemMapper serveOrderItemMapper;
    @Resource
    private StockService stockService;

    @Resource
    private ServeOrderLogMapper serveOrderLogMapper;

    @Override
    public Page<ServeOrderItem> findPage(ServeOrderItemFindPageForm form) {
        Page<ServeOrderItem> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?6:form.getPageSize());
        QueryWrapper<ServeOrderItem> query = new QueryWrapper<>();
        return serveOrderItemMapper.selectPage(page, query);
    }

    @Override
    public List<ServeOrderItem> findList(ServeOrderItemFindListForm form){
        QueryWrapper<ServeOrderItem> query = new QueryWrapper<>();
        query.eq("serve_order_id", form.getServeOrderId());
        return serveOrderItemMapper.selectList(query);
    }

    @Override
    public ServeOrderItem findById(Integer id) {
        return serveOrderItemMapper.selectById(id);
    }

    @Override
    public ServeOrderItem insert(ServeOrderItemInsertForm form) {
        ServeOrderItem serveOrderItem = BeanUtil.toBean(form, ServeOrderItem.class);
        boolean save = save(serveOrderItem);
        if (!save) {
            throw new WoniuException(500, "新增失败");
        }
        return serveOrderItem;
    }

    @Override
    public ServeOrderItem update(ServeOrderItemUpdateForm form) {
        ServeOrderItem serveOrderItem = BeanUtil.toBean(form, ServeOrderItem.class);
        boolean b = updateById(serveOrderItem);
        if (!b) {
            throw new WoniuException(500, "修改失败");
        }
        return serveOrderItem;
    }

    @Override
    public void delete(Integer id) {
        int i=serveOrderItemMapper.deleteById(id);
        if (i == 0) {
            throw new WoniuException(500, "删除失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void over(Integer id) {
        ServeOrderItem serveOrderItem = serveOrderItemMapper.selectById(id);
        if (serveOrderItem == null) {
            throw new WoniuException(500, "服务订单进度不存在");
        }
        if (serveOrderItem.getState() != 0) {
            throw new WoniuException(500, "服务订单进度状态异常");
        }
        QueryWrapper<ServeOrderItem> qw = new QueryWrapper<>();
        qw.eq("serve_order_id", serveOrderItem.getServeOrderId());
        qw.eq("state", 0);
        qw.lt("sort", serveOrderItem.getSort());
        List<ServeOrderItem> serveOrderItems = serveOrderItemMapper.selectList(qw);
        if (serveOrderItems != null && serveOrderItems.size() != 0) {
            throw new WoniuException(500, "该服务订单进度之前，还有未完成的服务订单进度");
        }
        serveOrderItem.setState(1);
        serveOrderItemMapper.updateById(serveOrderItem);
        //减门店库存
        StockUpdateParam param = new StockUpdateParam();
        String token = WebUtil.getToken();
        Integer clerkId = JwtUtil.getClerkId(token);
        param.setAdminId(clerkId);
        param.setStock(1);
        param.setId(serveOrderItem.getServeItemId());
        stockService.reduce(param);
        //生成服务订单日志
        ServeOrderLog serveOrderLog = new ServeOrderLog();
        serveOrderLog.setServeOrderId(serveOrderItem.getServeOrderId());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        serveOrderLog.setLogTime(LocalDateTime.now().format(formatter));
        serveOrderLog.setLogDesc("员工对用户进行服务");
        serveOrderLog.setLogUser(clerkId);
        serveOrderLogMapper.insert(serveOrderLog);
    }

}